<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/elementUI.css">
    <script src="../js/vue.js"></script>
    <script src="../js/axios.js"></script>
    <script src="../js/elementUI.js"></script>
    <script src="../../../../../lemeng_web_portal/src/main/webapp/js/util.js"></script>

</head>
<body>

<div id="app">

    <el-input v-model="query.name" placeholder="请输入规格名称" clearable style="width: 200px;"></el-input>
    <el-button type="primary" @click="toAdd">添加</el-button>
    <el-button type="danger" @click="delAll">批量删除</el-button>
    <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号">
        </el-table-column>
        <el-table-column
                prop="name"
                label="规格名称">
        </el-table-column>
        <el-table-column
                prop="options"
                label="规格选项"
                show-overflow-tooltip>
        </el-table-column>
        <el-table-column
                prop="seq"
                label="排序"
                show-overflow-tooltip>
        </el-table-column>


        <el-table-column label="模板">
            <template slot-scope="scope">
                {{tname}}
            </template>
        </el-table-column>

        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>

    </el-table>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[10, 20, 30,40]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog :title="mode.title+'规格'" :visible.sync="dialogFormVisible" top="10px" :before-close="beforeCloseDialog">
        <el-form :model="form">
            <el-form-item label="名称" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="排序" :label-width="formLabelWidth">
                <el-input v-model="form.seq" type="number" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="选项" :label-width="formLabelWidth">
                <el-input v-model="form.options1" type="textarea"  :autosize="{ minRows: 2, maxRows: 4}" autocomplete="off"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="addBrand">确 定</el-button>
        </div>
    </el-dialog>

</div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            tname:"",
            mode:{
                title:"",
                url:""
            },
            fileList:[],
            formLabelWidth:"120px",
            query:{
                name:"",
                page:1,
                size:10
            },
            form:{
                name:"",
            },
            tableData:[],
            total:0,
            multipleSelection:[],
            dialogImageUrl: '',
            dialogVisible: false,
            dialogFormVisible:false
        },
        watch:{
            query:{
                deep:true,
                handler(){
                    this.initData()
                }
            }
        },
        methods: {
            goPara(index,row){
                //window.open("")
                location.href = "para.html?tid="+row.id;
            },
            goSpec(index,row){
                location.href = "spec.html?tid="+row.id;
            },
            handleEdit(index, row) {
                this.mode = {
                    title:"修改",
                    url:"/spec/update.do"
                }
                row.options1 = "";
                this.form = JSON.parse(JSON.stringify(row));//普通输入框的回显
                this.form.options1 = this.form.options.replace(/,/g,"\n")
                this.dialogFormVisible = true;
            },
            delAll(){
                if(this.multipleSelection.length>0){
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.delBrand(this.multipleSelection.map(m=>m.id).join(","));
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }else{
                    this.$message('请选择一条数据');
                }
            },
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.delBrand(row.id);
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            delBrand(ids){
                axios.get(`/spec/deleteAll.do?ids=${ids}`).then(res=>{
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.initData()
                })

            },
            beforeCloseDialog(close){

                this.initFormData();
                close()//关闭弹窗的方法
            },
            addBrand(){
                this.form.templateId = this.query.templateId;
                this.form.options = this.form.options1.replace(/\n/g,",")
                axios.post(this.mode.url,this.form).then(res=>{
                    this.initFormData();
                    this.initData()
                })
            },
            toAdd(){
                this.mode = {
                    title:"添加",
                    url:"/spec/add.do"
                }
                this.dialogFormVisible = true;
            },
            initFormData(){
                this.form = {
                    image:"",
                    name:"",
                    seq:"",
                    letter:""
                }
                this.fileList = [];
                this.dialogFormVisible = false;

            },
            initData(){
                axios.post(`/spec/findPage.do?page=${this.query.page}&size=${this.query.size}`,this.query).then(res=>{
                    this.tableData = res.data.rows;
                    this.total = res.data.total;
                    if(this.tableData.length==0&&this.query.page>1){
                        this.query.page--
                    }
                })

            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.query.size = val
            },
            handleCurrentChange(val) {
                this.query.page = val
            },
            handleRemove(file, fileList) {
                this.form.image = "";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            uploadSuccess(res){
                this.form.image = res;
            }
        },
        created() {
            console.log(location.href)
            // location.href.substring(location.href.indexOf("=")+1)
            // location.href.split("tid=")[1]
            // location.search.split("=")[1]

            // this.query.templateId = location.search.split("=")[1]
            this.query.templateId = getQueryString("tid")
            this.tname = getQueryString("tname")
            this.initData();
        }
    })
</script>
</html>